package com.yanqu.road.dao.impl.cross.cricket;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.cricket.court.CricketCourtData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossCricketCourtDaoImpl extends TempDataObjectDao<CricketCourtData> {

    public Map<Integer, CricketCourtData> getCricketCourtDataMap() {
        Map<Integer, CricketCourtData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String selectSql = "select * from t_c_cricket_court_data ";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CricketCourtData data = parse(rs);
                map.put(data.getRound(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String insertSql = "insert into `t_c_cricket_court_data`(round,generateGroup,rankReward,rewardTime,endTime,updateTime) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(CricketCourtData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<>();
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        dbParameterMap.put(i++, new DbParameter(Types.BOOLEAN, data.isGenerateGroup()));
        dbParameterMap.put(i++, new DbParameter(Types.BOOLEAN, data.isRankReward()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getRewardTime()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getEndTime()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    private static String updateSql = "update `t_c_cricket_court_data` set `generateGroup`=?,`rankReward`=?,`rewardTime`=?,`endTime`=?,`updateTime`=? where round=?";
    @Override
    protected boolean updateData(CricketCourtData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<>();
        dbParameterMap.put(i++, new DbParameter(Types.BOOLEAN, data.isGenerateGroup()));
        dbParameterMap.put(i++, new DbParameter(Types.BOOLEAN, data.isRankReward()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getRewardTime()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getEndTime()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        if (getDb().executeNoneQuery(updateSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CricketCourtData parseData(ResultSet rs) throws SQLException {
        CricketCourtData data = new CricketCourtData();
        data.setRound(rs.getInt("round"));
        data.setGenerateGroup(rs.getBoolean("generateGroup"));
        data.setRankReward(rs.getBoolean("rankReward"));
        data.setRewardTime(rs.getLong("rewardTime"));
        data.setEndTime(rs.getLong("endTime"));
        data.setUpdateTime(rs.getLong("updateTime"));
        return data;
    }
}
